<template>
  <div>
    <van-nav-bar title="京东登录" left-text="返回" left-arrow @click-left="onClickLeft" />

    <van-cell-group inset>
      <van-field v-model="checktel" name="username" label="手机号" placeholder="手机号"
        :rules="[{ required: true, message: '请填写手机号' }]" />
      <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]" />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit" @click="toLogin">
        提交
      </van-button>
    </div>
    <span @click="gotoreguster">还没有账号？，去注册</span>
  </div>
</template>

<script>
import { doLogin } from '@/apis/wode'
export default {
  data() {
    return {
      checktel: '',
      password: ''
    }
  },
  methods: {
    onClickLeft() {
      history.back()
    },
    toLogin() {
      doLogin({ loginname: this.checktel, password: this.password }).then(res => {
        console.log(res);
        if (res.data.code == 10010) {
          return showToast("该用户不存在")
        } else if (res.data.code == 10011) {
          return showToast("密码错误")
        } else {
          showToast("登录成功")
          localStorage.setItem("token", res.data.data.token)
          localStorage.setItem("userid", res.data.data.userid)
          setTimeout(() => {
            this.$router.push("/home")
          }, 1000)
        }
      })
    },
    gotoreguster(){
      this.$router.push("/register")
    }
  },
  created() {
    this.checktel = localStorage.getItem("tel")
  }
}
</script>

<style lang="less" scoped></style>